#include "stdafx.h"
#include "Enemy.h"

//IplImage * CEnemy::m_enemyimg = cvLoadImage("");
CEnemy::CEnemy()
{
	m_HP = 30;
}


CEnemy::~CEnemy()
{
}


void CEnemy::LoadImg(IplImage * pimg[])
{
	for (int i = 0; i < ENEMY_IMG_COUNT; i++)
		m_enemyImg[i] = pimg[i];
}


void CEnemy::Release()
{
	//cvReleaseImage(&m_enemyimg);
}


void CEnemy::DrawtoBK(IplImage * pbk)
{
	if (m_beginCounter > m_currentCounter)
		return;

	if (pbk == NULL || m_enemyImg[m_state] == NULL)
		return;

	cvLine(pbk, CvPoint(m_pos.x, m_pos.y - 4),
		CvPoint(m_pos.x + m_HP, m_pos.y - 4),
		CvScalar(0, 0, 255), 3);

	for (int i = 0; i < m_enemyImg[m_state]->height; i++)
	{
		for (int j = 0; j < m_enemyImg[m_state]->width; j++)
		{

			int srcr, srcg, srcb;
			srcb = CV_IMAGE_ELEM(m_enemyImg[m_state], uchar, i, j * 3 + 0);
			srcg = CV_IMAGE_ELEM(m_enemyImg[m_state], uchar, i, j * 3 + 1);
			srcr = CV_IMAGE_ELEM(m_enemyImg[m_state], uchar, i, j * 3 + 2);

			if ((srcg > 250) && (srcb == 0) && (srcr == 0))
				continue;
			if ((m_pos.y + i) >= pbk->height)
				continue;

			if ((m_pos.x + j) >= pbk->width)
				continue;

			CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 0) = srcb;
			CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 1) = srcg;
			CV_IMAGE_ELEM(pbk, uchar, m_pos.y + i, (j + m_pos.x) * 3 + 2) = srcr;

		}
	}
	 
}


void CEnemy::InitPos(CvPoint bpos, int begincounter)
{
	m_beginCounter = begincounter;
	m_dir = -1;
	m_pos.x = bpos.x * 32;
	m_pos.y = bpos.y * 32;
	m_state = 0;
}


void CEnemy::Move(int dir, int curcount)
{
	m_currentCounter = curcount;
	if (m_beginCounter > m_currentCounter)
		return;
	m_dir = dir;
	switch (dir)
	{
	case 0:
		m_pos.y -= 4;
		m_state = 0;
		break;
	case 1:
		m_pos.y += 4;
		m_state = 1;
		break;
	case 2:
		m_pos.x -= 4;
		m_state = 2;		
		break;
	case 3:
		m_pos.x += 4;
		m_state = 3;
		break;
	}
}
